
import api from '@/api'
import Vue from 'vue'
import cxivm from '../cxivmInstall'
import util from '../util.js'
import Cookies from 'js-cookie'

Vue.use(cxivm.toastPlugin)
let S = new Vue()

const actions = {
    /**
     * 登录
     * @param {Vm} Vm vue对象
     * @param {stirng} key_id
     * @param {string | number} type 1学生 2教职工
     */
    async login({state}, params) {
        // 设置环境
        Vue.prototype.environment = util.judeEnv()
        if (params.Vm.environment.isFengChao) {
            // 调用JS-API登录
            await params.Vm.$cjc.login({ appKey: '0000' })
            params.Vm.$cjc.hiddenNavigation()
            let userInfo = await params.Vm.$cjc.getUserInfo()
            return userInfo
        } else if (params.Vm.environment.isXXMH) {
            // 获取RSA用以标识从信息门户进入
            let RSA = Cookies.get('combStr')
            let userInfo = await params.Vm.$store.dispatch('getUserInfoByRSA', {
                combStr: RSA
            })
            return userInfo
        } else {
            alert('请将客户端升级至最新版~')
        }
    },
    /**
     * 从信息门户进入 根据RSA获取数据
     */
    async getUserInfoByRSA({commit}, params) {
        let userInfo = await api.getUserInfoByRSA(params)
        return userInfo
    },
    /**
     * 保存用户信息
    */
    async setRegisterInfo({commit}, params) {
        let saveUserInfo = await api.saveUserInfo(params)
        if (saveUserInfo.data.status !== 1000) {
            S.$vux.toast.text(saveUserInfo.data.msg, 'bottom')
            return Promise.reject(saveUserInfo)
        } else {
            commit('SET_registInfo', saveUserInfo.data.result)
        }
    },
    /**
     * 完成某个步骤
    */
    async modifiedStep({commit}, params) {
        let modifiedStep = await api.modifiedStep(params)
        if (modifiedStep.data.status !== 1000) {
            S.$vux.toast.text(modifiedStep.data.msg, 'bottom')
            return Promise.reject(modifiedStep)
        } else {
            return modifiedStep
        }
    },
    /**
     * 分页设置寝室列表数据
    */
    async setDormitoryList({commit}, params) {
        let DormitoryList = await api.getDormitoryListByKsh(params)
        if (DormitoryList.data.status !== 1000) {
            S.$vux.toast.text(DormitoryList.data.msg, 'bottom')
            return Promise.reject(DormitoryList)
        } else {
            if (DormitoryList.data.result.length !== 0) {
                commit('SET_DormitoryList', DormitoryList.data.result)
            }
            return DormitoryList
        }
    },
    /**
     * 分页设置寝室列表数据
    */
    async comfirmDormitory({commit}, params) {
        let comfirmDormitory = await api.comfirmDormitory(params)
        if (comfirmDormitory.data.status !== 1000) {
            S.$vux.toast.text(comfirmDormitory.data.msg, 'bottom')
            return Promise.reject(comfirmDormitory)
        } else {
            return comfirmDormitory
        }
    },
    /**
     * 电子注册单
     */
    async getRegistInfo({commit}, params) {
        let RegistInfo = await api.getRegistInfo(params)
        return RegistInfo
    }
}

export default actions
